package com.igeek.boot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.anno.WMSLoganno;
import com.igeek.boot.constant.RedisConstant;
import com.igeek.boot.entity.Assets;
import com.igeek.boot.entity.Shelves;
import com.igeek.boot.result.Result;
import com.igeek.boot.service.IAssetsService;
import com.igeek.boot.service.IShelvesService;
import com.igeek.boot.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-12-22
 */
@RestController
@RequestMapping("/wisdom_factory/assets")
@Api(tags = "物资表接口")
@Slf4j
public class AssetsController {

    @Autowired
    private IAssetsService assetsService;

    @Autowired
    private IShelvesService shelfService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    //查询物资表信息
    @GetMapping("/findAll")
    @ApiOperation("查询物资表所有信息")
    public Result findAll(){
        List<Assets> list = assetsService.findAll();
        return new Result(true,"查询成功",list);
    }

    //根据物资id查询物资的详细信息
    @GetMapping("/findOneById/{id}")
    @ApiOperation("根据物资id查询物资的详细信息")
    public Result findOneById(@PathVariable("id") Integer id){
        Assets assets = assetsService.findOneById(id);
        if(!ObjectUtils.isEmpty(assets)){
            return new Result(true,"查询成功",assets);
        }
        return new Result(true,"查询失败",assets);
    }

    //分页查询物资表的数据
    @ApiOperation("条件分页查询检查项")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页条件") PageQueryVO pageQueryVO){
        try {
            Page<Assets> page = assetsService.findPage(pageQueryVO);
            List<Assets> collect = page.getRecords().stream().map(assets -> {
                String shelfName = shelfService.findOneById(assets.getShelfId()).getShelfName();
                if(shelfName!= null){
                    assets.setShelfName(shelfService.findOneById(assets.getShelfId()).getShelfName());
                }
                return assets;
            }).collect(Collectors.toList());
            page.setRecords(collect);
            if(!ObjectUtils.isEmpty(page)) return new Result(true,"查询成功",page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,"查询失败");
    }

    //添加物资的信息
    @WMSLoganno(module = "物资",operation = "添加物资")
    @ApiOperation("添加物资信息")
    @PostMapping("/add")
    public Result add(@RequestBody Assets assets){
        try {
            boolean flag = assetsService.add(assets);
            if(flag) return new Result(true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,"添加失败");
    }

    //更新物资表的信息
    @ApiOperation("更新物资表信息")
    @WMSLoganno(module = "物资",operation = "更新物资")
    @PutMapping("/update")
    public Result update(@RequestBody Assets assets){
        try {
            boolean flag = assetsService.updateById(assets);
            if(flag) return new Result(true,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,"更新失败");
    }

    //删除物资信息
    @WMSLoganno(module = "物资",operation = "删除物资")
    @ApiOperation("删除物资信息")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try {
            boolean flag = assetsService.removeById(id);
            if(flag) return new Result(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }

    //根据物资的名称类型和货架id查询物资信息
    @GetMapping("/selectByShelfIdAndNameType")
    @ApiOperation("根据物资的名称类型和货架id查询物资信息")
    public Result selectByShelfIdAndNameType(@RequestParam("shelfID") Integer shelfID,
                                             @RequestParam("name") String name,
                                             @RequestParam("type") String type){
        Assets assets = assetsService.selectByShelfIdAndNameType(shelfID,name,type);
        if(!ObjectUtils.isEmpty(assets)){
            return new Result(true,"查询物资信息成功",assets);
        }else {
            return new Result(false,"查询失败");
        }
    }


    //向redis中存入库存信息
    @ApiOperation("添加库存信息")
    @GetMapping("/addAssetsToRedis")
    public Result addAssetsToRedis(){
        String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String beforeDay = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //存入今天Redis中的库存信息  --> 获取当物资的信息，并将信息传入到redis中
        List<Assets> assets = assetsService.findAll();
        for (Assets asset : assets) {
            redisTemplate.opsForList().leftPushAll(RedisConstant.VERIFICATION_ASSET_INVENTORY+beforeDay,asset);
        }
        //取出数据
        List<Object> assets1 = redisTemplate.opsForList().range(RedisConstant.VERIFICATION_ASSET_INVENTORY+beforeDay,0,-1);
        return new Result(true,"添加成功",assets1);
    }
}
